package cn.dhnep.demo.controller;


import cn.dhnep.demo.pojo.Result;
import cn.dhnep.demo.pojo.entity.Guizi;
import cn.dhnep.demo.pojo.entity.Information;
import cn.dhnep.demo.pojo.entity.LongitudeAndLatitudeEntity;
import cn.dhnep.demo.util.LongitudeAndLatitude;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author 邱文彬
 * @Date 2021/11/9 12:48
 * @Version 1.0
 */


@RestController
public class LongitudeAndLatitudeController {
    @RequestMapping(value = "getLongitudeAndLatitude", method = RequestMethod.GET)
    public @ResponseBody
    Result getLongitudeAndLatitude(@RequestParam("lng") BigDecimal lng, @RequestParam("lat") BigDecimal lat) throws UnknownHostException {

        List<Guizi> longitudeAndLatitudeAndName = getLongitudeAndLatitudeAndName();
        LongitudeAndLatitudeEntity longitudeAndLatitudeOne = new LongitudeAndLatitudeEntity();
        longitudeAndLatitudeOne.setLatitude(lat);
        longitudeAndLatitudeOne.setLongitude(lng);

        List<Double> list = new ArrayList<Double>();


        for (int i = 0; i < longitudeAndLatitudeAndName.size();i++){
            BigDecimal lat1 = longitudeAndLatitudeAndName.get(i).getLat();
            BigDecimal lng1 = longitudeAndLatitudeAndName.get(i).getLng();
            LongitudeAndLatitudeEntity longitudeAndLatitudeTwo = new LongitudeAndLatitudeEntity();
            longitudeAndLatitudeTwo.setLatitude(lat1);
            longitudeAndLatitudeTwo.setLongitude(lng1);
            double longitudeAndLatitude = LongitudeAndLatitude.getLongitudeAndLatitude(longitudeAndLatitudeOne, longitudeAndLatitudeTwo);
            list.add(i,longitudeAndLatitude);
        }

        Double Double1 = list.get(0);
        System.out.println("初始: " + Double1);
        int num = 0;
        for(int x = 0 ; x <= list.size()+2;x++){
            if(x+1 < 56){
                if(list.get(x+1) < Double1){
                    Double1 = list.get(x+1);
                    num = (x+1);
                }
            }


        }
        System.out.println("结束: " +Double1);
        String name = longitudeAndLatitudeAndName.get(num).getName();
        BigDecimal lat1 = longitudeAndLatitudeAndName.get(num).getLat();
        BigDecimal lng1 = longitudeAndLatitudeAndName.get(num).getLng();
        String formatted_address = longitudeAndLatitudeAndName.get(num).getFormatted_address();

        System.out.println(name);
        String distance;
        if(Double1 > 1000){
            Double1 /= 1000;
            distance = Double1 +  "公里";
        }else{
            distance = Double1 +  "米";
        }
        System.out.println("最终返回 :" + distance);
        for (Double aDouble : list) {

        }
        return new Result(0,"操作成功",new Information(distance,name,lat1,lng1,formatted_address));

    }


    public static  List<Guizi> getLongitudeAndLatitudeAndName() {
        String str = get("https://rest.hive-intel.com/web/sensors?token=6114de1fe25583000137d3dc");
        JSONObject jsonArray = JSONArray.parseObject(str);
        JSONArray content = (JSONArray) jsonArray.get("content");


        List<Guizi> list = new ArrayList<>();
        for(int i = 0; i < content.size();i++){

            JSONObject jsonArray1 = (JSONObject) content.get(i);
            String name = (String)jsonArray1.get("name");
            JSONObject position1 = (JSONObject) jsonArray1.get("position");
            BigDecimal lng = (BigDecimal) position1.get("lng");
            BigDecimal lat = (BigDecimal) position1.get("lat");
            JSONObject position2 = (JSONObject) jsonArray1.get("meta");
            String formatted_address = (String)position2.get("formatted_address");

            Guizi guizi = new Guizi();
            guizi.setLat(lat);
            guizi.setName(name);
            guizi.setLng(lng);
            guizi.setFormatted_address(formatted_address);
            list.add(i,guizi);
        }

        return list;
    }

    public static String get(String url) {
        try {
            URL thisurl = new URL(url); // 把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) thisurl
                    .openConnection();// 打开连接
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接

            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
